/*
* @Author: ZHELIHAO\fangshuqin
* @Date:   2018-05-16 16:09:52
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-22 09:54:14
*/

{
	let EVENT_CLICK = Laya.Event.CLICK;
	class ModifyNick extends ui.Alert.ModifyNickUI{

		constructor(kind,nick, sex, diamond, currentDiamond, cb) {
			super();
			this.kind = kind;    //0创建账号 1修改昵称
			this.nick = nick || '';
			this.sex =  sex  || -1;
			this.diamond= diamond || 0;
			this.currentDiamond = currentDiamond || 0;
			this.callback = cb || null;
			this.init();
		}

		init() {
			this.dom_nick.text = this.nick;
			this.dom_diamond.text = this.diamond;
			this.tab_sex.selectedIndex = this.sex;

			this.nick_title.index= this.kind;
			
			if(this.kind && this.diamond>0){
				this.dom_diamond.visible = true;
				this.button.index= 1;
			}else{
				this.dom_diamond.visible = false;
				this.button.index= 0;
			}

			this.registerActions();
			this.initEvent();

			this.success = false;  

		}


        registerActions() {

            this.ACTIONS = {
                [GAME_CMDS.GETRANDNICK] : this.nickInfo,
                [GAME_CMDS.SETNICKANDSEX] : this.setNick
            };
            Sail.io.register(this.ACTIONS, this);
        }

		initEvent() {
			this.btn_refresh.on(EVENT_CLICK, this, ()=>{
				if(this.dom_tips){
					this.dom_tips.visible = false;
				}

				Sail.io.emit(GAME_CMDS.GETRANDNICK);
				
			})

			this.btn_confirm.on(EVENT_CLICK, this, ()=>{
				if(this.diamond >= this.currentDiamond){
					Sail.director.popScene(new Alert.Tips('钻石不足，请充值'));
					return;
				}
				let sex = this.sex;
				let nickName = this.dom_nick.text;
				let diamond = this.dom_diamond.text;
				console.log("sex nickName", sex, nickName,diamond);
				Sail.io.emit(GAME_CMDS.SETNICKANDSEX,{"nickName": nickName,"sex": sex, "diamond" : diamond});
				
			})

			/*this.bg_nick.on(EVENT_CLICK, this, ()=>{
				this.dom_tips.visible = false;
				this.dom_nick.text = '';
			})*/

			this.tab_sex.selectHandler = new Laya.Handler(this, (index) => {
				this.sex = index;    //0男 1女 2保密
            });
		}

		nickInfo(data, code, msg) {
			if(code !==0 ){return;}
			this.dom_nick.text = data;
		}

		setNick(data,code, msg) {
			if(msg){
				this.dom_tips.text = msg;
				this.dom_tips.visible = true;
				return;
			}
			this.success = true;
			this.close();
			Sail.io.unregister(this.ACTIONS);

		}

		onUpdate() {
			if(this.success){
				let nick = this.dom_nick.text;
				let sex  = this.sex;
				this.callback && this.callback(nick,sex);
			}
		}

		onClosed() {
			Sail.io.unregister(this.ACTIONS);
			this.onUpdate();
		}
	}

	Sail.class(ModifyNick, 'Alert.ModifyNick');
}